package game.com.game.action;

import game.com.game.Commpany;
import game.com.game.ICommpanyListener;
import game.com.game.Persion;

/**
 * Created by maizhipeng on 2017/5/3.
 */

public class WorkAction extends AbstractAction {

    Commpany mCommpany;

    public WorkAction(Commpany commpany) {
        mCommpany = commpany;
    }

    @Override
    public void start(final Persion persion) {
        if (persion.getX() != mCommpany.getX() || persion.getY() != mCommpany.getY()) {
            WalkAction walkAction = new WalkAction(persion.getX(), persion.getY(), mCommpany.getX(), mCommpany.getY());
            walkAction.setIActionListener(new IActionListener() {

                @Override
                public void onFinish() {
                    startWorking(persion);

                }
            });
            walkAction.start(persion);
        } else {
            startWorking(persion);

        }


    }

    private void startWorking(final Persion persion) {
        persion.setStatus(Persion.WORK_SATUS);
        mCommpany.startEarnMoney(persion, new ICommpanyListener() {
            @Override
            public void onWorkFinish(long earnMoney) {
                persion.addMoney(earnMoney);
                persion.setStatus(Persion.NORMAL_STATUS);
                if (mIActionListener != null) mIActionListener.onFinish();
            }
        });
    }

}
